<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/**
 * Product view template
 *
 * @see Mage_Catalog_Block_Product_View
 * @see Mage_Review_Block_Product_View
 */
?>
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>

<?php
$brandId = $_product->getBrand();
$productId = $_product->getId();
$sameBrandProducts = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect(array('small_image','name','price'))
                        ->addAttributeToFilter('visibility', array('neq' => 1))
                        ->addAttributeToFilter('status', 1)
                        ->addAttributeToFilter('entity_id', array('neq' => $productId))
                        ->addAttributeToFilter('brand',$brandId)
                        ->setCurPage(1)
                        ->setPageSize(4);
Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->sameBrandProducts);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($sameBrandProducts);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($sameBrandProducts);
?>
<style>
	.brand-product .product-image img{
		max-width: 57%;
	}
	.brand-product .regular-price .price{
		color: #ef3c42;
	}

</style>
<div class="same-title">
    <span>同品牌推荐</span>
	<div class="same-title-line"></div>
</div>
<?php if(count($sameBrandProducts)>0): ?>
<div class="same-brand">
    
    <ul class="brand-content clearfix slides">
        <?php foreach($sameBrandProducts as $product): ?>
            <li class="brand-product">
            
                <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>">
                    <div class="product-image">
                        <img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize('150'); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />
                    </div>
                </a>
            
            <div>
                
                    <a class="name" href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>">
                        <?php 
                        $name = $_helper->productAttribute($product, $product->getName(), 'name');
                        $encoding = mb_internal_encoding();
                        mb_internal_encoding("UTF-8");
                        if(mb_strlen($name) >25){
                            echo mb_substr($name,0,25).'...';
                        }else{
                            echo $name;
                        }
                        mb_internal_encoding($encoding);
                        ?>
                    </a>
                    <?php echo $this->getPriceHtml($product, true) ?>
                
            </div>
            </li>
        <?php endforeach; ?>
    </ul>
</div>
<div class="brandicon">
<div class="prev">上一页</div>
<div class="next">下一页</div>
</div>
<?php else:?>
    <div class="same-brand">
        <p>没有相应产品</p>
    </div>
<?php endif; ?>
<?php if(count($sameBrandProducts)>2): ?>
<script type="text/javascript">
jQuery(function() {
	var page = 1;
	var i = 2;
	var conheight = jQuery('.same-brand .brand-content li').height();
	console.log(conheight);
	var lilen = jQuery('.brand-content li').length;
	var len = Math.ceil( lilen - i )+1;
	jQuery('.brandicon .next').click(function() {
	if( page == len ) {
	jQuery('.brand-content').animate({top:0},'slow');
	page = 1;
	}else{
	jQuery('.brand-content').animate({top:'-=' + conheight},'slow');
	page++;
	}
});
jQuery('.brandicon .prev').click(function() {
if( page == 1 ) {
jQuery('.brand-content').animate({top:'-=' + conheight*(len - 1)},'slow');
page = len;
}else{
jQuery('.brand-content').animate({top:'+=' + conheight},'slow');
page--;
}
});
})
</script>
<?php else:?>
<script type="text/javascript">
    jQuery(function(){
        var conheight = jQuery('.same-brand .brand-content li').height();
        var lilen = jQuery('.brand-content li').length;
        var hei = conheight*lilen;
        jQuery('.same-brand').css('min-height',hei);
        jQuery('.brandicon').hide();
    });
</script>
<?php endif; ?>
